home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / BitTorrent / download.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  12.5 KB  |  306 lines

  1. # Written by Bram Cohen
  2. # see LICENSE.txt for license information
  3.  
  4. from zurllib import urlopen
  5. from urlparse import urljoin
  6. from btformats import check_message
  7. from Choker import Choker
  8. from Storage import Storage
  9. from StorageWrapper import StorageWrapper
  10. from Uploader import Upload
  11. from Downloader import Downloader
  12. from Connecter import Connecter
  13. from Encrypter import Encoder
  14. from RawServer import RawServer
  15. from Rerequester import Rerequester
  16. from DownloaderFeedback import DownloaderFeedback
  17. from RateMeasure import RateMeasure
  18. from CurrentRateMeasure import Measure
  19. from PiecePicker import PiecePicker
  20. from bencode import bencode, bdecode
  21. from __init__ import version
  22. from binascii import b2a_hex
  23. from sha import sha
  24. from os import path, makedirs
  25. from parseargs import parseargs, formatDefinitions
  26. from socket import error as socketerror
  27. from random import seed
  28. from threading import Thread, Event
  29. from time import time
  30. try:
  31.     from os import getpid
  32. except ImportError:
  33.     def getpid():
  34.         return 1
  35.  
  36. defaults = [
  37.     ('max_uploads', 7,
  38.         "the maximum number of uploads to allow at once."),
  39.     ('keepalive_interval', 120.0,
  40.         'number of seconds to pause between sending keepalives'),
  41.     ('download_slice_size', 2 ** 14,
  42.         "How many bytes to query for per request."),
  43.     ('request_backlog', 5,
  44.         "how many requests to keep in a single pipe at once."),
  45.     ('max_message_length', 2 ** 23,
  46.         "maximum length prefix encoding you'll accept over the wire - larger values get the connection dropped."),
  47.     ('ip', '',
  48.         "ip to report you have to the tracker."),
  49.     ('minport', 6881, 'minimum port to listen on, counts up if unavailable'),
  50.     ('maxport', 6999, 'maximum port to listen on'),
  51.     ('responsefile', '',
  52.         'file the server response was stored in, alternative to url'),
  53.     ('url', '',
  54.         'url to get file from, alternative to responsefile'),
  55.     ('saveas', '',
  56.         'local file name to save the file as, null indicates query user'),
  57.     ('timeout', 300.0,
  58.         'time to wait between closing sockets which nothing has been received on'),
  59.     ('timeout_check_interval', 60.0,
  60.         'time to wait between checking if any connections have timed out'),
  61.     ('max_slice_length', 2 ** 17,
  62.         "maximum length slice to send to peers, larger requests are ignored"),
  63.     ('max_rate_period', 20.0,
  64.         "maximum amount of time to guess the current rate estimate represents"),
  65.     ('bind', '', 
  66.         'ip to bind to locally'),
  67.     ('upload_rate_fudge', 5.0, 
  68.         'time equivalent of writing to kernel-level TCP buffer, for rate adjustment'),
  69.     ('display_interval', .5,
  70.         'time between updates of displayed information'),
  71.     ('rerequest_interval', 5 * 60,
  72.         'time to wait between requesting more peers'),
  73.     ('min_peers', 20, 
  74.         'minimum number of peers to not do rerequesting'),
  75.     ('http_timeout', 60, 
  76.         'number of seconds to wait before assuming that an http connection has timed out'),
  77.     ('max_initiate', 35,
  78.         'number of peers at which to stop initiating new connections'),
  79.     ('max_allow_in', 55,
  80.         'maximum number of connections to allow, after this new incoming connections will be immediately closed'),
  81.     ('check_hashes', 1,
  82.         'whether to check hashes on disk'),
  83.     ('max_upload_rate', 0,
  84.         'maximum kB/s to upload at, 0 means no limit'),
  85.     ('snub_time', 30.0,
  86.         "seconds to wait for data to come in over a connection before assuming it's semi-permanently choked"),
  87.     ('spew', 0,
  88.         "whether to display diagnostic info to stdout"),
  89.     ('rarest_first_cutoff', 4,
  90.         "number of downloads at which to switch from random to rarest first"),
  91.     ('min_uploads', 4,
  92.         "the number of uploads to fill out to with extra optimistic unchokes"),
  93.     ('report_hash_failures', 0,
  94.         "whether to inform the user that hash failures occur. They're non-fatal."),
  95.     ]
  96.  
  97. def download(params, filefunc, statusfunc, finfunc, errorfunc, doneflag, cols, pathFunc = None, paramfunc = None, spewflag = Event()):
  98.     if len(params) == 0:
  99.         errorfunc('arguments are -\n' + formatDefinitions(defaults, cols))
  100.         return
  101.     try:
  102.         config, args = parseargs(params, defaults, 0, 1)
  103.         if args:
  104.             if config.get('responsefile', None) == None:
  105.                 raise ValueError, 'must have responsefile as arg or parameter, not both'
  106.             if path.isfile(args[0]):
  107.                 config['responsefile'] = args[0]
  108.             else: 
  109.                 config['url'] = args[0]
  110.         if (config['responsefile'] == '') == (config['url'] == ''):
  111.             raise ValueError, 'need responsefile or url'
  112.     except ValueError, e:
  113.         errorfunc('error: ' + str(e) + '\nrun with no args for parameter explanations')
  114.         return
  115.     
  116.     try:
  117.         if config['responsefile'] != '':
  118.             h = open(config['responsefile'], 'rb')
  119.         else:
  120.             h = urlopen(config['url'])
  121.         response = h.read()
  122.         h.close()
  123.     except IOError, e:
  124.         if config['responsefile'] != '' and config['responsefile'].find('Temporary Internet Files') != -1:
  125.             errorfunc('BitTorrent was passed a filename that doesn\'t exist.  ' +
  126.                 'Either clear your Temporary Internet Files or right-click the link ' + 
  127.                 'and save the .torrent to disk first.')
  128.         else:
  129.             errorfunc('problem getting response info - ' + str(e))
  130.         return
  131.  
  132.     try:
  133.         response = bdecode(response)
  134.         check_message(response)
  135.     except ValueError, e:
  136.         errorfunc("got bad file info - " + str(e))
  137.         return
  138.     
  139.     try:
  140.         def make(f, forcedir = False):
  141.             if not forcedir:
  142.                 f = path.split(f)[0]
  143.             if f != '' and not path.exists(f):
  144.                 makedirs(f)
  145.                 
  146.         info = response['info']
  147.         if info.has_key('length'):
  148.             file_length = info['length']
  149.             file = filefunc(info['name'], file_length, config['saveas'], False)
  150.             if file is None:
  151.                 return
  152.             make(file)
  153.             files = [(file, file_length)]
  154.         else:
  155.             file_length = 0
  156.             for x in info['files']:
  157.                 file_length += x['length']
  158.             file = filefunc(info['name'], file_length, config['saveas'], True)
  159.             if file is None:
  160.                 return
  161.   
  162.             # if this path exists, and no files from the info dict exist, we assume it's a new download and 
  163.             # the user wants to create a new directory with the default name
  164.             existing = 0
  165.             if path.exists(file):
  166.                 for x in info['files']:
  167.                     if path.exists(path.join(file, x['path'][0])):
  168.                         existing = 1
  169.                 if not existing:
  170.                     file = path.join(file, info['name'])
  171.                     
  172.             make(file, True)
  173.             
  174.             # alert the UI to any possible change in path
  175.             if pathFunc != None:
  176.                 pathFunc(file)
  177.                 
  178.             files = []
  179.             for x in info['files']:
  180.                 n = file
  181.                 for i in x['path']:
  182.                     n = path.join(n, i)
  183.                 files.append((n, x['length']))
  184.                 make(n)
  185.     except OSError, e:
  186.         errorfunc("Couldn't allocate dir - " + str(e))
  187.         return
  188.     
  189.     finflag = Event()
  190.     ann = [None]
  191.     myid = 'M' + version.replace('.', '-')
  192.     myid = myid + ('-' * (8 - len(myid))) + b2a_hex(sha(repr(time()) + ' ' + str(getpid())).digest()[-6:])
  193.     seed(myid)
  194.     pieces = [info['pieces'][x:x+20] for x in xrange(0, 
  195.         len(info['pieces']), 20)]
  196.     def failed(reason, errorfunc = errorfunc, doneflag = doneflag):
  197.         doneflag.set()
  198.         if reason is not None:
  199.             errorfunc(reason)
  200.     rawserver = RawServer(doneflag, config['timeout_check_interval'], config['timeout'], errorfunc = errorfunc, maxconnects = config['max_allow_in'])
  201.     try:
  202.         try:
  203.             storage = Storage(files, open, path.exists, path.getsize)
  204.         except IOError, e:
  205.             errorfunc('trouble accessing files - ' + str(e))
  206.             return
  207.         def finished(finfunc = finfunc, finflag = finflag, 
  208.                 ann = ann, storage = storage, errorfunc = errorfunc):
  209.             finflag.set()
  210.             try:
  211.                 storage.set_readonly()
  212.             except (IOError, OSError), e:
  213.                 errorfunc('trouble setting readonly at end - ' + str(e))
  214.             if ann[0] is not None:
  215.                 ann[0](1)
  216.             finfunc()
  217.         rm = [None]
  218.         def data_flunked(amount, rm = rm, errorfunc = errorfunc, report_hash_failures = config['report_hash_failures']):
  219.             if rm[0] is not None:
  220.                 rm[0](amount)
  221.             if report_hash_failures:
  222.                 errorfunc('a piece failed hash check, re-downloading it')
  223.         storagewrapper = StorageWrapper(storage, 
  224.             config['download_slice_size'], pieces, 
  225.             info['piece length'], finished, failed, 
  226.             statusfunc, doneflag, config['check_hashes'], data_flunked)
  227.     except ValueError, e:
  228.         failed('bad data - ' + str(e))
  229.     except IOError, e:
  230.         failed('IOError - ' + str(e))
  231.     if doneflag.isSet():
  232.         return
  233.  
  234.     e = 'maxport less than minport - no ports to check'
  235.     for listen_port in xrange(config['minport'], config['maxport'] + 1):
  236.         try:
  237.             rawserver.bind(listen_port, config['bind'])
  238.             break
  239.         except socketerror, e:
  240.             pass
  241.     else:
  242.         errorfunc("Couldn't listen - " + str(e))
  243.         return
  244.  
  245.     choker = Choker(config['max_uploads'], rawserver.add_task, finflag.isSet, 
  246.         config['min_uploads'])
  247.     upmeasure = Measure(config['max_rate_period'], 
  248.         config['upload_rate_fudge'])
  249.     downmeasure = Measure(config['max_rate_period'])
  250.     def make_upload(connection, choker = choker, 
  251.             storagewrapper = storagewrapper, 
  252.             max_slice_length = config['max_slice_length'],
  253.             max_rate_period = config['max_rate_period'],
  254.             fudge = config['upload_rate_fudge']):
  255.         return Upload(connection, choker, storagewrapper, 
  256.             max_slice_length, max_rate_period, fudge)
  257.     ratemeasure = RateMeasure(storagewrapper.get_amount_left())
  258.     rm[0] = ratemeasure.data_rejected
  259.     picker = PiecePicker(len(pieces), config['rarest_first_cutoff'])
  260.     for i in xrange(len(pieces)):
  261.         if storagewrapper.do_I_have(i):
  262.             picker.complete(i)
  263.     downloader = Downloader(storagewrapper, picker,
  264.         config['request_backlog'], config['max_rate_period'],
  265.         len(pieces), downmeasure, config['snub_time'], 
  266.         ratemeasure.data_came_in)
  267.     connecter = Connecter(make_upload, downloader, choker,
  268.         len(pieces), upmeasure, config['max_upload_rate'] * 1024, rawserver.add_task)
  269.     infohash = sha(bencode(info)).digest()
  270.     encoder = Encoder(connecter, rawserver, 
  271.         myid, config['max_message_length'], rawserver.add_task, 
  272.         config['keepalive_interval'], infohash, config['max_initiate'])
  273.     rerequest = Rerequester(response['announce'], config['rerequest_interval'], 
  274.         rawserver.add_task, connecter.how_many_connections, 
  275.         config['min_peers'], encoder.start_connection, 
  276.         rawserver.add_task, storagewrapper.get_amount_left, 
  277.         upmeasure.get_total, downmeasure.get_total, listen_port, 
  278.         config['ip'], myid, infohash, config['http_timeout'], errorfunc, 
  279.         config['max_initiate'], doneflag, upmeasure.get_rate, downmeasure.get_rate,
  280.         encoder.ever_got_incoming)
  281.     if config['spew']:
  282.         spewflag.set()
  283.     DownloaderFeedback(choker, rawserver.add_task, statusfunc, 
  284.         upmeasure.get_rate, downmeasure.get_rate, 
  285.         upmeasure.get_total, downmeasure.get_total, ratemeasure.get_time_left, 
  286.         ratemeasure.get_size_left, file_length, finflag,
  287.         config['display_interval'], spewflag)
  288.  
  289.  
  290.     # useful info and functions for the UI
  291.     if paramfunc:
  292.         paramfunc({ 'max_upload_rate' : connecter.change_max_upload_rate,  # change_max_upload_rate(<int bytes/sec>)
  293.                     'max_uploads': choker.change_max_uploads, # change_max_uploads(<int max uploads>)
  294.                     'listen_port' : listen_port, # int
  295.                     'peer_id' : myid, # string
  296.                     'info_hash' : infohash, # string
  297.                     'start_connection' : encoder._start_connection # start_connection((<string ip>, <int port>), <peer id>)
  298.                     })
  299.     
  300.     statusfunc({"activity" : 'connecting to peers'})
  301.     ann[0] = rerequest.announce
  302.     rerequest.begin()
  303.     rawserver.listen_forever(encoder)
  304.     storage.close()
  305.     rerequest.announce(2)
  306.